#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 30;

int n;
vector<int> piles[N];
void init() {
  rep(i, 0, n - 1) {
    piles[i].clear();
    piles[i].push_back(i);
  }
}
void clear(int i, int h) {
  int len = piles[i].size();
  rep(j, h + 1, len - 1) {
    int k = piles[i][j];
    piles[k].push_back(k);
  }
  piles[i].resize(h + 1);
}
void find(int x, int& p, int& h) {
  rep(i, 0, n - 1) {
    int len = piles[i].size();
    rep(j, 0, len - 1) {
      if (piles[i][j] == x) {
        p = i, h = j;
        return;
      }
    }
  }
}
void pile_onto(int p, int h, int p2) {
  int len = piles[p].size();
  rep(i, h, len - 1) piles[p2].push_back(piles[p][i]);
  piles[p].resize(h);
}
void go(string op1, int a, string op2, int b) {
  int i1, h1, i2, h2;
  find(a, i1, h1), find(b, i2, h2);
  if (i1 == i2) return;
  if (op2 == "onto") clear(i2, h2);
  if (op1 == "move") clear(i1, h1);
  pile_onto(i1, h1, i2);
}
void show() {
  rep(i, 0, n - 1) {
    cout << i << ':';
    int len = piles[i].size();
    rep(j, 0, len - 1) cout << ' ' << piles[i][j];
    cout << endl;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  init();
  string op, op2;
  int a, b;
  while (cin >> op && op != "quit") {
    cin >> a >> op2 >> b;
    go(op, a, op2, b);
  }
  show();
  return 0;
}